package com.kotlin.zds


import android.app.Activity
import com.alibaba.android.arouter.launcher.ARouter
import com.kotlin.zds.glide.GlideImageLoader
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.constant.SpinnerStyle
import com.xuexiang.xui.XUI
import com.zds.support.BuildConfig
import com.zds.support.base.SupportBaseApplication
import com.zds.support.gilde.ImageLoaderManager
import com.zds.support.util.AppUtils
import java.util.*

/**
 * Author: zds
 * Date: 2022/6/10 18:50
 * Description:
 * Version: v1.0
 */
class AppApplication : SupportBaseApplication(){
    override fun onCreate() {
        ImageLoaderManager.instance.init(applicationContext, GlideImageLoader())
        AppUtils.appContext = this
        AppUtils.isDebug = AppUtils.getIsDebugMode(applicationContext, BuildConfig.DEBUG)
        if(AppUtils.isDebug) {
            XUI.debug(true);  //开启UI框架调试日志
            ARouter.openLog()
            ARouter.openDebug()
        }

        XUI.init(this)
        ARouter.init(this)
        super.onCreate()
    }

    override fun initConfig() {
        super.initConfig()
        initSmartRefreshLayout()
    }

    private fun initSmartRefreshLayout() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            // WaterDropHeader(context)/*.setSpinnerStyle(SpinnerStyle.Translate)*/
            MaterialHeader(context)/*.setSpinnerStyle(SpinnerStyle.Translate)*/
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            //指定为经典Footer，默认是 BallPulseFooter
            // BallPulseFooter(context).setSpinnerStyle(SpinnerStyle.Translate)
            ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate)
        }

    }

    companion object{
        lateinit var instance:AppApplication
        //页面堆栈管理
        private var activityStack: Stack<Activity> = Stack()
        /**
         *@desc:添加activity
         *@author:zhangyz
         *@created at: 2022/3/27 12:16
         **/
        fun addActivify(activity: Activity){
            activityStack.push(activity)
        }
        /**
         *@desc:结束所有activity
         *@author:zhangyz
         *@created at: 2022/3/27 12:16
         **/
        fun finishAllActivity(){
            for (activity:Activity in activityStack){
                activity.finish()
            }
        }
        /**
         *@desc:结束指定activity
         *@author:zhangyz
         *@created at: 2022/4/2 11:06
         **/
        fun finishActivity(activity: Activity){
            activityStack.remove(activity)
            if (!activity.isFinishing){
                activity.finish()
            }
        }

        /**
         *@desc:结束指定类名的Activity
         *@author:zhangyz
         *@created at: 2022/4/2 11:15
         **/
        fun finishActivity(cls: Class<*>){
            var iterator= activityStack.iterator()
            while (iterator.hasNext()){
                var aty=iterator.next()
                if (aty.javaClass==cls){
                    finishActivity(aty)
                }
            }
        }
    }
}